<?php

namespace app\api\controller;

use app\common\lib\Common;
use think\Controller;
use think\facade\App;
use \think\facade\Db;

class Withdrawal extends Auth
{

    public function __construct()
    {
        parent::__construct();
        if ($this->role != 2) {
            $this->initreturn('越权操作');
        }
    }

    /**
     * 发起提现
     */
    public function addWithdrawal()
    {
        $post = input('post.');

        if (empty($post['amount']) || floatval($post['amount']) <= 0) {
            return $this->json([], 2000, '请输入正确提现金额');
        }

        if (empty($post['cardid'])) {
            return $this->json([], 2000, '请选择银行卡');
        }

        $cardwhere = ['cardid' => $post['cardid'], 'table'=>$this->table];
        $card = $this->findTable('bank_card', ['where' => $cardwhere]);
    
        $withdrawal = $this->findTable('withdrawal', ['where' => [
            'coachid' => $this->userid,
            'status' => 1
        ]]);

        if(! empty($withdrawal)) {
            return $this->json([], 2000, '您已有未处理的提现记录，请等待处理再提交');
        }
        
        if (empty($card)) {
            return $this->json([], 2000, '请选择正确的银行卡');
        }

        $where = ['coachid' => $this->userid, 'isdel' => 0, 'status' => 1];
        $info = $this->findTable('coach_amount', ['where' => $where]);

        if (empty($info)) {
            return $this->json([], 2000, '暂无可提现金额');
        }

        // if (bccomp($post['amount'], $info['totalamount'], 2) == -1) {
            if(floatval($post['amount']) >= floatval($info['totalamount'])) {
            return $this->json([], 2000, '可提现金额不足');
        }

        $data = [
            'cardnumber' => $card['cardnumber'],
            'cardid' => $card['cardid'],
            'coachid' => $this->userid,
            'totalprice' => $info['totalamount'],
            'amount' => $post['amount'],
            'balance' => bcsub($info['totalamount'], $post['amount'], 2),
        ];

        $ans = $this->insertTable('withdrawal', $data);
        if ($ans) {
            return $this->json($ans, 1001, '已成功发起申请');
        } else {
            return $this->json($post, 2000, '发起提现失败');
        }
    }

    /**
     * 获取金额
     */
    public function getWithdrawalAmount()
    {
        $where = ['coachid' => $this->userid, 'isdel' => 0, 'status' => 1];
        $info = $this->findTable('coach_amount', ['where' => $where]);

        if (empty($info)) {
            return $this->json(['totalamount' => 0.00], 1000);
        }

        return $this->json(['totalamount' => $info['totalamount']], 1000);
    }

    public function getWithdrawalList()
    {
        $get = input('get.');

        $data = $where = [];
        if (!empty($get['begin']) && !empty($get['end'])) {
            $begin = strtotime($get['begin']);
            $end = strtotime(date('Y-m-d', strtotime('+1 day', $get['end'])));
            $where[] = ['addtime', 'between', [$begin, $end]];
        }

        $data['where'] = $where;
        $data['tableName'] = 'withdrawal';
        $data['totalCount'] = $this->countTable('withdrawal', $data);
        $data = $this->buildCount($data);
        $list = $this->listTable('withdrawal', $data);

        foreach ($list as $key => $val) {
            $list[$key]['addtime'] = empty($val['addtime']) ? '' : date('Y-m-d H:i:s', $val['addtime']);
            $list[$key]['updatetime'] = empty($val['updatetime']) ? '' : date('Y-m-d H:i:s', $val['updatetime']);
        }
        $data['list'] = $list;
        return $this->json($data, 1000);
    }
}
